package com.bnz.controller;

import com.bnz.pojo.PageResult;
import com.bnz.pojo.R;
import com.bnz.pojo.Student;
import com.bnz.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    //1. 查询所有学生
    @GetMapping("list")
    public R findAll(){
        //1.1 查询所有学生
        List<Student> students = studentService.findAll();
        //1.2 返回数据给前端
        return R.ok().data("list",students);
    }

    //2. 分页查询
    @GetMapping("findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable int page,@PathVariable int pageSize){
        //2.1 开始分页查询
        PageResult<Student> pr = studentService.findByPage(page, pageSize);
        //2.2 返回数据给前端页面
        return R.ok().data("pr",pr);
    }

    //3. 条件查询带分页 @RequestBody:将前端传过来的json对象转化为Java对象
    @PostMapping("search/{page}/{pageSize}")
    public R search(@PathVariable int page,
                    @PathVariable int pageSize,
                    @RequestBody Student vo){
        //3.1 分页查询
        PageResult<Student> pr = studentService.search(page,pageSize,vo);
        //3.2 返回数据给前端
        return R.ok().data("pr",pr);
    }

    //4. 添加学生
    @PostMapping("add")
    public R add(@RequestBody Student student){
        studentService.add(student);
        return R.ok().message("添加学生成功！");
    }

    //5. 修改学生
    @PostMapping("update")
    public R update(@RequestBody Student student){
        studentService.update(student);
        return R.ok().message("修改学生成功！");
    }

    //6. 删除学生
    @DeleteMapping("delete/{ids}")
    public R delete(@PathVariable Integer[] ids){
        studentService.delete(ids);
        return R.ok().message("删除学生成功！");
    }
}
